
dirr = '/home/chris/school/AtoA/odl-acous-artic/data/M1/';
files = listfiles(strcat(dirr, '/textgrid/'));
files = extstrp(files, 'TextGrid');
files = sort(files);
n_files = numel(files);

Ts_t = 0.005;
Windows_t = 0.005:0.005:0.2;
ACOUSTICS = cell(n_files, numel(Windows_t));

%EMA = cell(numel(Fils), 1);

for i = 1:numel(Windows_t)
    mkdir(strcat(dirr, '/acoustic_features/', int2str(Windows_t(i)*1000)));
end

        
for i = 1:n_files;
    
    file = files{i};
    ema = dlmread(strcat(dirr, 'tables_corrected/', file, '.txt'));
    samples = length(ema);
    [a, Fs] = wavread(strcat(dirr, 'wav/', file, '.wav'));
    
    if i == 1
        Ts_s = round(Ts_t*Fs);
        Windows_s = Windows_t*Fs;
    end
    
    for j = 1:numel(Windows_t)
        window_s = round(Windows_s(j));

        w = window(@hann, window_s);
        
        AC = zeros(samples, 49);
        starts = linspace(1, samples, samples);
        starts = (starts*Ts_s)-(Ts_s-1);
        fins = starts+window_s-1;
        
        %here the edges of the audio file gets padded with very low
        %intensity noise at the edge if its too short, or samples at the 
        %edge deleted if its too long
        
        pad = fins(samples)-length(a);
        if pad < 0
            A = a(floor(abs(pad/2))+1:length(a)-ceil(abs(pad/2)) );      
        elseif pad > 0
            A = vertcat( random('norm', 0, 0.001, [floor(pad/2), 1]), a, random('norm', 0, 0.001, [ceil(pad/2), 1]) );
        else
            A = a;
        end
            
        for k = 1:samples
            frame = A(starts(k):fins(k));
            
            
            %Gettin the features
            
            rms = sqrt(mean(frame.^2));
            frame = w.*frame;
            mfccs = melcepst(frame, Fs, 'R', 16, floor(3*log(Fs)), window_s, Ts_s);
            lpcs =  lpc(frame, 16);
            lsfs = poly2lsf(lpcs);
            AC(k, 1:16) = lpcs(2:17); AC(k, 17:32) = mfccs; AC(k, 33:48) = lsfs'; AC(k, 49) = rms;
        end
        
        
        %calculatin derivatives
        
        
        dAC = AC(2:samples, :)-AC(1:samples-1, :);
        dAC = vertcat(dAC(1, :), dAC);
        ddAC = dAC(2:samples, :)-dAC(1:samples-1, :);
        ddAC = vertcat(ddAC(1, :), ddAC);
    
        AC = horzcat(AC, dAC, ddAC);
        dlmwrite(strcat(dirr, 'acoustic_features/', int2str(Windows_t(j)*1000), '/', file, '.txt'), AC, '\t');        
        
        ppp = j
    end
    
    zzz = i
end